package com.jyb.model;

import java.util.Date;
import java.util.List;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Model;
import com.jyb.util.UUIDUtils;

public class QueryLog extends Model<QueryLog>{

	private static final long serialVersionUID = 8818673840790381574L;
	public static final QueryLog dao = new QueryLog();
	
	public void addOrUpdate(String userId,String lx,String value){
		String sql="select * from t_query_log t where t.userId=? and t.lx=? and t.value=?";
		QueryLog queryLog=this.findFirst(sql,userId,lx,value);
		Date now=new Date();
		if(queryLog==null){
			sql="insert into t_query_log (id,createTime,updateTime,userId,lx,value) values(?,?,?,?,?,?)";
			Db.update(sql,UUIDUtils.getUUID(),now,now,userId,lx,value);
		}else{
			queryLog.set("updateTime", now);
			queryLog.update();
		}
	}
	
	public List<QueryLog> findQueryLogsByUserId(String userId,String lx,Integer limit){
		String sql="select * from t_query_log t where t.userId=? and t.createTime>? and lx=? order by t.updateTime desc limit ? ";
		return this.find(sql,userId,new Date(System.currentTimeMillis()-1000*60*60*24*10),lx,limit);//10天以内的
	}
}
